<template>
  <div>
    <template>
      <Card style="width:850px;margin: auto;">
        <Form :model="formLeft" label-position="left" :label-width="100" style="width: 850px;">

          <FormItem label="产品代码" style="margin-left: 25%;width: 300px">
            <Select v-model="productName">
              <Option v-for="item in productList" :value="item.name">{{ item.name }}</Option>
            </Select>
          </FormItem>

          <Button type="dashed" long @click="handleAdd" icon="md-add" style="width: 98%;margin-bottom: 20px">添加材料
          </Button>
          <Row>

            <Form ref="formDynamic" :model="formDynamic">
              <FormItem
                v-for="(item, index) in formDynamic.items"
                v-if="item.status"
                :key="index"
                :prop="'items.' + index + '.value'">
                <Row>
                  <div style="width: 360px;float: left">
                    <FormItem label="物料名称">
                      <Select v-model="item.materielNo" style="width: 210px" filterable>
                        <Option v-for="item in materialType" :value="item.typeName">{{ item.typeName }}</Option>
                      </Select>
                    </FormItem>
                  </div>

                  <span class="key">重量:  </span>
                  <Input v-model="item.weight" style="width: 80px;margin-left: 10px"
                         @on-keyup="inputChange"
                         @on-keydown="inputChange" @on-blur="inputChange"></Input>

                  <span class="key" style="margin-left: 50px">数量:</span>
                  <Input v-model="item.quantity" style="width: 80px;margin-left: 10px"
                         @on-keyup="inputHandler" @on-keydown="inputHandler" @on-blur="inputHandler"></Input>


                  <Button @click="handleRemove(index)" type="error" style="margin-left: 50px">
                    删除
                  </Button>
                </Row>
              </FormItem>
            </Form>
          </Row>


          <row type="flex" justify="center">
            <i-col span="20">

            </i-col>
            <i-col span="30">
              <Button type="primary" @click="BomInsert">添加</Button>
              <Button type="success" style="margin-left: 160px" @click="back">返回</Button>
            </i-col>
          </row>

        </Form>
      </Card>
    </template>
  </div>
</template>

<script>
  import {BomInsert, MaterialTypeFindAll, ProductFindAll} from "../../../api/data"

  export default {
    data() {
      return {
        index: 1,
        index1: 1,
        formDynamic: {
          items: [
            {
              index: 0,
              status: 1,
              materielNo: '',
              quantity: 0,
              weight: 0,
              productName: ''
            }
          ],
        },
        page: 1,//page 第几页
        pageSize: 1000,//pageSize:每页几条数据
        total: 0,
        materialType: [],
        productList: [],
        productName: '',
        formLeft: {
          productName: '',
          materielNo: '',
          quantity: 0,
          weight: 0,
        },
      }
    },
    mounted() {
      this.ProductFindAll();
      this.MaterialTypeFindAll();
    },
    methods: {
      inputChange(e) {
        //z只能输入一次小数点
        e.target.value = e.target.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        //只能输入数字
        e.target.value = e.target.value.replace(/[^\d.]/g, "");
      },
      inputHandler(e) {
        //只能输入0-9
        e.target.value = e.target.value.replace(/[^0-9]/g, '')
      },
      BomInsert() {
        let state = 'true';
        //添加物料
        for (let i = 0; i < this.formDynamic.items.length; i++) {
          this.formDynamic.items[i].productName = this.productName;
          if (this.formDynamic.items[i].materielNo !== null && this.formDynamic.items[i].materielNo !== ""
            && this.formDynamic.items[i].productName !== null && this.formDynamic.items[i].productName !== "") {
            if (this.formDynamic.items[i].weight == 0 && this.formDynamic.items[i].quantity == 0) {
              this.$Message.error("数量和重量不能都为0");
            } else {
              BomInsert(this.formDynamic.items[i]).then(res => {
                if (res.data.code == 0) {
                  this.$Message.success("添加成功");
                } else {
                  state = 'false';
                  this.$Message.error(res.data.msg);
                }
              })
            }
          } else {
            this.$Message.error("物料名称为空！！！");
          }
        }
        if (state=='true'){
          this.$router.go(-1)
        }
      },
      //产品代码
      ProductFindAll() {
        var data = {
          page: this.page,
          pageSize: this.pageSize,
        };
        ProductFindAll(data).then(data => {
          if (data.data.code == 0) {
            this.productList = data.data.data.list;
          }
        })
      },
      //物料名称
      MaterialTypeFindAll() {
        var data = {
          page: this.page,
          pageSize: this.pageSize,
        };
        MaterialTypeFindAll(data).then(data => {
          if (data.data.code == 0) {
            this.materialType = data.data.data.list;
          }
        })
      },
      back() {
        this.$router.go(-1);
      },
      handleAdd() {
        this.index++;
        this.formDynamic.items.push({
          index: this.index,
          status: 1,
        });
      },
      handleRemove(index) {
        this.formDynamic.items.splice(index, 1);
        --this.index;
      },

    }
  }
</script>


